<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlsec - Perl security - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlsec">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlsec.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlsec">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlsec">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlsec">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlsec">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlsec">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlsec">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlsec">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlsec">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlsec">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlsec">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlsec">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlsec">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlsec">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlsec">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlsec">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlsec">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlsec">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlsec">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlsec">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlsec">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlsec">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlsec">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlsec">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlsec">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlsec">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlsec">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlsec">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlsec">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlsec">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlsec">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlsec">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlsec">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlsec">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlsec">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlsec">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlsec">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlsec">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlsec">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlsec">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlsec">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlsec">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlsec">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlsec">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlsec">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlsec">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlsec">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlsec">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlsec">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlsec">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlsec">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlsec">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlsec">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlsec">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlsec">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlsec">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlsec">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlsec">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlsec">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlsec">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlsec">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlsec">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlsec">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlsec">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlsec">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlsec">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlsec">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlsec">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlsec">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlsec">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlsec">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlsec">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlsec">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlsec">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlsec">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlsec">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlsec">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlsec">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlsec">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlsec">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlsec">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlsec.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlsec">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlsec">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlsec">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlsec">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlsec">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlsec">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlsec">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlsec">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlsec">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlsec">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlsec">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlsec">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlsec">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlsec">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlsec">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlsec">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlsec">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlsec">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlsec">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlsec">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlsec">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlsec">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlsec">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlsec">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlsec">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlsec">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlsec">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlsec">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlsec">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlsec">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlsec">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlsec">5.35.1</a>
          <a class="dropdown-item active" href="perlsec.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlsec.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlsec.html">perlsec</a>
          <div id="more">
            (<a href="perlsec.txt">source</a>,
            <a href="https://metacpan.org/pod/perlsec">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlsec.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#SECURITY-VULNERABILITY-CONTACT-INFORMATION">SECURITY VULNERABILITY CONTACT INFORMATION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#SECURITY-MECHANISMS-AND-CONCERNS">SECURITY MECHANISMS AND CONCERNS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Taint-mode">Taint mode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Laundering-and-Detecting-Tainted-Data">Laundering and Detecting Tainted Data</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Switches-On-the-%22%23!%22-Line">Switches On the &quot;#!&quot; Line</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Taint-mode-and-@INC">Taint mode and @INC</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Cleaning-Up-Your-Path">Cleaning Up Your Path</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Shebang-Race-Condition">Shebang Race Condition</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Protecting-Your-Programs">Protecting Your Programs</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Unicode">Unicode</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Algorithmic-Complexity-Attacks">Algorithmic Complexity Attacks</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#Using-Sudo">Using Sudo</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlsec.html#SEE-ALSO">SEE ALSO</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlsec.html#NAME">#</a>NAME</h1>

<p>perlsec - Perl security</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlsec.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>Perl is designed to make it easy to program securely even when running with extra privileges, like setuid or setgid programs. Unlike most command line shells, which are based on multiple substitution passes on each line of the script, Perl uses a more conventional evaluation scheme with fewer hidden snags. Additionally, because the language has more builtin functionality, it can rely less upon external (and possibly untrustworthy) programs to accomplish its purposes.</p>

<h1 id="SECURITY-VULNERABILITY-CONTACT-INFORMATION"><a class="permalink" href="perlsec.html#SECURITY-VULNERABILITY-CONTACT-INFORMATION">#</a><a id="SECURITY"></a>SECURITY VULNERABILITY CONTACT INFORMATION</h1>

<p>If you believe you have found a security vulnerability in the Perl interpreter or modules maintained in the core Perl codebase, email the details to <a href="mailto:perl-security@perl.org">perl-security@perl.org</a>. This address is a closed membership mailing list monitored by the Perl security team.</p>

<p>See <a href="perlsecpolicy.html">perlsecpolicy</a> for additional information.</p>

<h1 id="SECURITY-MECHANISMS-AND-CONCERNS"><a class="permalink" href="perlsec.html#SECURITY-MECHANISMS-AND-CONCERNS">#</a><a id="SECURITY1"></a>SECURITY MECHANISMS AND CONCERNS</h1>

<h2 id="Taint-mode"><a class="permalink" href="perlsec.html#Taint-mode">#</a><a id="Taint"></a>Taint mode</h2>

<p>Perl automatically enables a set of special security checks, called <i>taint mode</i>, when it detects its program running with differing real and effective user or group IDs. The setuid bit in Unix permissions is mode 04000, the setgid bit mode 02000; either or both may be set. You can also enable taint mode explicitly by using the <b>-T</b> command line flag. This flag is <i>strongly</i> suggested for server programs and any program run on behalf of someone else, such as a CGI script. Once taint mode is on, it&#39;s on for the remainder of your script.</p>

<p>While in this mode, Perl takes special precautions called <i>taint checks</i> to prevent both obvious and subtle traps. Some of these checks are reasonably simple, such as verifying that path directories aren&#39;t writable by others; careful programmers have always used checks like these. Other checks, however, are best supported by the language itself, and it is these checks especially that contribute to making a set-id Perl program more secure than the corresponding C program.</p>

<p>You may not use data derived from outside your program to affect something else outside your program--at least, not by accident. All command line arguments, environment variables, locale information (see <a href="perllocale.html">perllocale</a>), results of certain system calls (<code>readdir()</code>, <code>readlink()</code>, the variable of <code>shmread()</code>, the messages returned by <code>msgrcv()</code>, the password, gcos and shell fields returned by the <code>getpwxxx()</code> calls), and all file input are marked as &quot;tainted&quot;. Tainted data may not be used directly or indirectly in any command that invokes a sub-shell, nor in any command that modifies files, directories, or processes, <b>with the following exceptions</b>:</p>

<ul>

<li><p>Arguments to <code>print</code> and <code>syswrite</code> are <b>not</b> checked for taintedness.</p>

</li>
<li><p>Symbolic methods</p>

<pre><code>$obj-&gt;$method(@args);</code></pre>

<p>and symbolic sub references</p>

<pre><code>&amp;{$foo}(@args);
$foo-&gt;(@args);</code></pre>

<p>are not checked for taintedness. This requires extra carefulness unless you want external data to affect your control flow. Unless you carefully limit what these symbolic values are, people are able to call functions <b>outside</b> your Perl code, such as POSIX::system, in which case they are able to run arbitrary external code.</p>

</li>
<li><p>Hash keys are <b>never</b> tainted.</p>

</li>
</ul>

<p>For efficiency reasons, Perl takes a conservative view of whether data is tainted. If an expression contains tainted data, any subexpression may be considered tainted, even if the value of the subexpression is not itself affected by the tainted data.</p>

<p>Because taintedness is associated with each scalar value, some elements of an array or hash can be tainted and others not. The keys of a hash are <b>never</b> tainted.</p>

<p>For example:</p>

<pre><code>    $arg = shift;		# $arg is tainted
    $hid = $arg . &#39;bar&#39;;	# $hid is also tainted
    $line = &lt;&gt;;			# Tainted
    $line = &lt;STDIN&gt;;		# Also tainted
    open FOO, &quot;/home/me/bar&quot; or die $!;
    $line = &lt;FOO&gt;;		# Still tainted
    $path = $ENV{&#39;PATH&#39;};	# Tainted, but see below
    $data = &#39;abc&#39;;		# Not tainted

    system &quot;echo $arg&quot;;		# Insecure
    system &quot;/bin/echo&quot;, $arg;	# Considered insecure
				# (Perl doesn&#39;t know about /bin/echo)
    system &quot;echo $hid&quot;;		# Insecure
    system &quot;echo $data&quot;;	# Insecure until PATH set

    $path = $ENV{&#39;PATH&#39;};	# $path now tainted

    $ENV{&#39;PATH&#39;} = &#39;/bin:/usr/bin&#39;;
    delete @ENV{&#39;IFS&#39;, &#39;CDPATH&#39;, &#39;ENV&#39;, &#39;BASH_ENV&#39;};

    $path = $ENV{&#39;PATH&#39;};	# $path now NOT tainted
    system &quot;echo $data&quot;;	# Is secure now!

    open(FOO, &quot;&lt; $arg&quot;);	# OK - read-only file
    open(FOO, &quot;&gt; $arg&quot;); 	# Not OK - trying to write

    open(FOO,&quot;echo $arg|&quot;);	# Not OK
    open(FOO,&quot;-|&quot;)
	or exec &#39;echo&#39;, $arg;	# Also not OK

    $shout = `echo $arg`;	# Insecure, $shout now tainted

    unlink $data, $arg;		# Insecure
    umask $arg;			# Insecure

    exec &quot;echo $arg&quot;;		# Insecure
    exec &quot;echo&quot;, $arg;		# Insecure
    exec &quot;sh&quot;, &#39;-c&#39;, $arg;	# Very insecure!

    @files = &lt;*.c&gt;;		# insecure (uses readdir() or similar)
    @files = glob(&#39;*.c&#39;);	# insecure (uses readdir() or similar)

    # In either case, the results of glob are tainted, since the list of
    # filenames comes from outside of the program.

    $bad = ($arg, 23);		# $bad will be tainted
    $arg, `true`;		# Insecure (although it isn&#39;t really)</code></pre>

<p>If you try to do something insecure, you will get a fatal error saying something like &quot;Insecure dependency&quot; or &quot;Insecure $ENV{PATH}&quot;.</p>

<p>The exception to the principle of &quot;one tainted value taints the whole expression&quot; is with the ternary conditional operator <code>?:</code>. Since code with a ternary conditional</p>

<pre><code>$result = $tainted_value ? &quot;Untainted&quot; : &quot;Also untainted&quot;;</code></pre>

<p>is effectively</p>

<pre><code>if ( $tainted_value ) {
    $result = &quot;Untainted&quot;;
} else {
    $result = &quot;Also untainted&quot;;
}</code></pre>

<p>it doesn&#39;t make sense for <code>$result</code> to be tainted.</p>

<h2 id="Laundering-and-Detecting-Tainted-Data"><a class="permalink" href="perlsec.html#Laundering-and-Detecting-Tainted-Data">#</a><a id="Laundering"></a>Laundering and Detecting Tainted Data</h2>

<p>To test whether a variable contains tainted data, and whose use would thus trigger an &quot;Insecure dependency&quot; message, you can use the <code>tainted()</code> function of the Scalar::Util module, available in your nearby CPAN mirror, and included in Perl starting from the release 5.8.0. Or you may be able to use the following <code>is_tainted()</code> function.</p>

<pre><code>sub is_tainted {
    local $@;   # Don&#39;t pollute caller&#39;s value.
    return ! eval { eval(&quot;#&quot; . substr(join(&quot;&quot;, @_), 0, 0)); 1 };
}</code></pre>

<p>This function makes use of the fact that the presence of tainted data anywhere within an expression renders the entire expression tainted. It would be inefficient for every operator to test every argument for taintedness. Instead, the slightly more efficient and conservative approach is used that if any tainted value has been accessed within the same expression, the whole expression is considered tainted.</p>

<p>But testing for taintedness gets you only so far. Sometimes you have just to clear your data&#39;s taintedness. Values may be untainted by using them as keys in a hash; otherwise the only way to bypass the tainting mechanism is by referencing subpatterns from a regular expression match. Perl presumes that if you reference a substring using $1, $2, etc. in a non-tainting pattern, that you knew what you were doing when you wrote that pattern. That means using a bit of thought--don&#39;t just blindly untaint anything, or you defeat the entire mechanism. It&#39;s better to verify that the variable has only good characters (for certain values of &quot;good&quot;) rather than checking whether it has any bad characters. That&#39;s because it&#39;s far too easy to miss bad characters that you never thought of.</p>

<p>Here&#39;s a test to make sure that the data contains nothing but &quot;word&quot; characters (alphabetics, numerics, and underscores), a hyphen, an at sign, or a dot.</p>

<pre><code>    if ($data =~ /^([-\@\w.]+)$/) {
	$data = $1; 			# $data now untainted
    } else {
	die &quot;Bad data in &#39;$data&#39;&quot;; 	# log this somewhere
    }</code></pre>

<p>This is fairly secure because <code>/\w+/</code> doesn&#39;t normally match shell metacharacters, nor are dot, dash, or at going to mean something special to the shell. Use of <code>/.+/</code> would have been insecure in theory because it lets everything through, but Perl doesn&#39;t check for that. The lesson is that when untainting, you must be exceedingly careful with your patterns. Laundering data using regular expression is the <i>only</i> mechanism for untainting dirty data, unless you use the strategy detailed below to fork a child of lesser privilege.</p>

<p>The example does not untaint <code>$data</code> if <code>use locale</code> is in effect, because the characters matched by <code>\w</code> are determined by the locale. Perl considers that locale definitions are untrustworthy because they contain data from outside the program. If you are writing a locale-aware program, and want to launder data with a regular expression containing <code>\w</code>, put <code>no locale</code> ahead of the expression in the same block. See <a href="perllocale.html#SECURITY">&quot;SECURITY&quot; in perllocale</a> for further discussion and examples.</p>

<h2 id="Switches-On-the-&quot;#!&quot;-Line"><a class="permalink" href="perlsec.html#Switches-On-the-%22%23!%22-Line">#</a><a id="Switches"></a><a id="Switches-On-the-Line"></a>Switches On the &quot;#!&quot; Line</h2>

<p>When you make a script executable, in order to make it usable as a command, the system will pass switches to perl from the script&#39;s #! line. Perl checks that any command line switches given to a setuid (or setgid) script actually match the ones set on the #! line. Some Unix and Unix-like environments impose a one-switch limit on the #! line, so you may need to use something like <code>-wU</code> instead of <code>-w -U</code> under such systems. (This issue should arise only in Unix or Unix-like environments that support #! and setuid or setgid scripts.)</p>

<h2 id="Taint-mode-and-@INC"><a class="permalink" href="perlsec.html#Taint-mode-and-@INC">#</a><a id="Taint1"></a><a id="Taint-mode-and-INC"></a>Taint mode and @INC</h2>

<p>When the taint mode (<code>-T</code>) is in effect, the environment variables <code>PERL5LIB</code> and <code>PERLLIB</code> are ignored by Perl. You can still adjust <code>@INC</code> from outside the program by using the <code>-I</code> command line option as explained in <a href="perlrun.html#-Idirectory">perlrun</a>. The two environment variables are ignored because they are obscured, and a user running a program could be unaware that they are set, whereas the <code>-I</code> option is clearly visible and therefore permitted.</p>

<p>Another way to modify <code>@INC</code> without modifying the program, is to use the <code>lib</code> pragma, e.g.:</p>

<pre><code class="plaintext">perl -Mlib=/foo program</code></pre>

<p>The benefit of using <code>-Mlib=/foo</code> over <code>-I/foo</code>, is that the former will automagically remove any duplicated directories, while the latter will not.</p>

<p>Note that if a tainted string is added to <code>@INC</code>, the following problem will be reported:</p>

<pre><code class="plaintext">Insecure dependency in require while running with -T switch</code></pre>

<p>On versions of Perl before 5.26, activating taint mode will also remove the current directory (&quot;.&quot;) from the default value of <code>@INC</code>. Since version 5.26, the current directory isn&#39;t included in <code>@INC</code> by default.</p>

<h2 id="Cleaning-Up-Your-Path"><a class="permalink" href="perlsec.html#Cleaning-Up-Your-Path">#</a><a id="Cleaning"></a>Cleaning Up Your Path</h2>

<p>For &quot;Insecure <code>$ENV{PATH}</code>&quot; messages, you need to set <code>$ENV{&#39;PATH&#39;}</code> to a known value, and each directory in the path must be absolute and non-writable by others than its owner and group. You may be surprised to get this message even if the pathname to your executable is fully qualified. This is <i>not</i> generated because you didn&#39;t supply a full path to the program; instead, it&#39;s generated because you never set your PATH environment variable, or you didn&#39;t set it to something that was safe. Because Perl can&#39;t guarantee that the executable in question isn&#39;t itself going to turn around and execute some other program that is dependent on your PATH, it makes sure you set the PATH.</p>

<p>The PATH isn&#39;t the only environment variable which can cause problems. Because some shells may use the variables IFS, CDPATH, ENV, and BASH_ENV, Perl checks that those are either empty or untainted when starting subprocesses. You may wish to add something like this to your setid and taint-checking scripts.</p>

<pre><code>delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer</code></pre>

<p>It&#39;s also possible to get into trouble with other operations that don&#39;t care whether they use tainted values. Make judicious use of the file tests in dealing with any user-supplied filenames. When possible, do opens and such <b>after</b> properly dropping any special user (or group!) privileges. Perl doesn&#39;t prevent you from opening tainted filenames for reading, so be careful what you print out. The tainting mechanism is intended to prevent stupid mistakes, not to remove the need for thought.</p>

<p>Perl does not call the shell to expand wild cards when you pass <code>system</code> and <code>exec</code> explicit parameter lists instead of strings with possible shell wildcards in them. Unfortunately, the <code>open</code>, <code>glob</code>, and backtick functions provide no such alternate calling convention, so more subterfuge will be required.</p>

<p>Perl provides a reasonably safe way to open a file or pipe from a setuid or setgid program: just create a child process with reduced privilege who does the dirty work for you. First, fork a child using the special <code>open</code> syntax that connects the parent and child by a pipe. Now the child resets its ID set and any other per-process attributes, like environment variables, umasks, current working directories, back to the originals or known safe values. Then the child process, which no longer has any special permissions, does the <code>open</code> or other system call. Finally, the child passes the data it managed to access back to the parent. Because the file or pipe was opened in the child while running under less privilege than the parent, it&#39;s not apt to be tricked into doing something it shouldn&#39;t.</p>

<p>Here&#39;s a way to do backticks reasonably safely. Notice how the <code>exec</code> is not called with a string that the shell could expand. This is by far the best way to call something that might be subjected to shell escapes: just never call the shell at all.</p>

<pre><code>        use English;
        die &quot;Can&#39;t fork: $!&quot; unless defined($pid = open(KID, &quot;-|&quot;));
        if ($pid) {           # parent
            while (&lt;KID&gt;) {
                # do something
            }
            close KID;
        } else {
            my @temp     = ($EUID, $EGID);
            my $orig_uid = $UID;
            my $orig_gid = $GID;
            $EUID = $UID;
            $EGID = $GID;
            # Drop privileges
            $UID  = $orig_uid;
            $GID  = $orig_gid;
            # Make sure privs are really gone
            ($EUID, $EGID) = @temp;
            die &quot;Can&#39;t drop privileges&quot;
                unless $UID == $EUID  &amp;&amp; $GID eq $EGID;
            $ENV{PATH} = &quot;/bin:/usr/bin&quot;; # Minimal PATH.
	    # Consider sanitizing the environment even more.
            exec &#39;myprog&#39;, &#39;arg1&#39;, &#39;arg2&#39;
                or die &quot;can&#39;t exec myprog: $!&quot;;
        }</code></pre>

<p>A similar strategy would work for wildcard expansion via <code>glob</code>, although you can use <code>readdir</code> instead.</p>

<p>Taint checking is most useful when although you trust yourself not to have written a program to give away the farm, you don&#39;t necessarily trust those who end up using it not to try to trick it into doing something bad. This is the kind of security checking that&#39;s useful for set-id programs and programs launched on someone else&#39;s behalf, like CGI programs.</p>

<p>This is quite different, however, from not even trusting the writer of the code not to try to do something evil. That&#39;s the kind of trust needed when someone hands you a program you&#39;ve never seen before and says, &quot;Here, run this.&quot; For that kind of safety, you might want to check out the Safe module, included standard in the Perl distribution. This module allows the programmer to set up special compartments in which all system operations are trapped and namespace access is carefully controlled. Safe should not be considered bullet-proof, though: it will not prevent the foreign code to set up infinite loops, allocate gigabytes of memory, or even abusing perl bugs to make the host interpreter crash or behave in unpredictable ways. In any case it&#39;s better avoided completely if you&#39;re really concerned about security.</p>

<h2 id="Shebang-Race-Condition"><a class="permalink" href="perlsec.html#Shebang-Race-Condition">#</a><a id="Shebang"></a>Shebang Race Condition</h2>

<p>Beyond the obvious problems that stem from giving special privileges to systems as flexible as scripts, on many versions of Unix, set-id scripts are inherently insecure right from the start. The problem is a race condition in the kernel. Between the time the kernel opens the file to see which interpreter to run and when the (now-set-id) interpreter turns around and reopens the file to interpret it, the file in question may have changed, especially if you have symbolic links on your system.</p>

<p>Some Unixes, especially more recent ones, are free of this inherent security bug. On such systems, when the kernel passes the name of the set-id script to open to the interpreter, rather than using a pathname subject to meddling, it instead passes <i>/dev/fd/3</i>. This is a special file already opened on the script, so that there can be no race condition for evil scripts to exploit. On these systems, Perl should be compiled with <code>-DSETUID_SCRIPTS_ARE_SECURE_NOW</code>. The <i>Configure</i> program that builds Perl tries to figure this out for itself, so you should never have to specify this yourself. Most modern releases of SysVr4 and BSD 4.4 use this approach to avoid the kernel race condition.</p>

<p>If you don&#39;t have the safe version of set-id scripts, all is not lost. Sometimes this kernel &quot;feature&quot; can be disabled, so that the kernel either doesn&#39;t run set-id scripts with the set-id or doesn&#39;t run them at all. Either way avoids the exploitability of the race condition, but doesn&#39;t help in actually running scripts set-id.</p>

<p>If the kernel set-id script feature isn&#39;t disabled, then any set-id script provides an exploitable vulnerability. Perl can&#39;t avoid being exploitable, but will point out vulnerable scripts where it can. If Perl detects that it is being applied to a set-id script then it will complain loudly that your set-id script is insecure, and won&#39;t run it. When Perl complains, you need to remove the set-id bit from the script to eliminate the vulnerability. Refusing to run the script doesn&#39;t in itself close the vulnerability; it is just Perl&#39;s way of encouraging you to do this.</p>

<p>To actually run a script set-id, if you don&#39;t have the safe version of set-id scripts, you&#39;ll need to put a C wrapper around the script. A C wrapper is just a compiled program that does nothing except call your Perl program. Compiled programs are not subject to the kernel bug that plagues set-id scripts. Here&#39;s a simple wrapper, written in C:</p>

<pre><code>#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;

#define REAL_PATH &quot;/path/to/script&quot;

int main(int argc, char **argv)
{
    execv(REAL_PATH, argv);
    fprintf(stderr, &quot;%s: %s: %s\n&quot;,
                    argv[0], REAL_PATH, strerror(errno));
    return 127;
}</code></pre>

<p>Compile this wrapper into a binary executable and then make <i>it</i> rather than your script setuid or setgid. Note that this wrapper isn&#39;t doing anything to sanitise the execution environment other than ensuring that a safe path to the script is used. It only avoids the shebang race condition. It relies on Perl&#39;s own features, and on the script itself being careful, to make it safe enough to run the script set-id.</p>

<h2 id="Protecting-Your-Programs"><a class="permalink" href="perlsec.html#Protecting-Your-Programs">#</a><a id="Protecting"></a>Protecting Your Programs</h2>

<p>There are a number of ways to hide the source to your Perl programs, with varying levels of &quot;security&quot;.</p>

<p>First of all, however, you <i>can&#39;t</i> take away read permission, because the source code has to be readable in order to be compiled and interpreted. (That doesn&#39;t mean that a CGI script&#39;s source is readable by people on the web, though.) So you have to leave the permissions at the socially friendly 0755 level. This lets people on your local system only see your source.</p>

<p>Some people mistakenly regard this as a security problem. If your program does insecure things, and relies on people not knowing how to exploit those insecurities, it is not secure. It is often possible for someone to determine the insecure things and exploit them without viewing the source. Security through obscurity, the name for hiding your bugs instead of fixing them, is little security indeed.</p>

<p>You can try using encryption via source filters (Filter::* from CPAN, or Filter::Util::Call and Filter::Simple since Perl 5.8). But crackers might be able to decrypt it. You can try using the byte code compiler and interpreter described below, but crackers might be able to de-compile it. You can try using the native-code compiler described below, but crackers might be able to disassemble it. These pose varying degrees of difficulty to people wanting to get at your code, but none can definitively conceal it (this is true of every language, not just Perl).</p>

<p>If you&#39;re concerned about people profiting from your code, then the bottom line is that nothing but a restrictive license will give you legal security. License your software and pepper it with threatening statements like &quot;This is unpublished proprietary software of XYZ Corp. Your access to it does not give you permission to use it blah blah blah.&quot; You should see a lawyer to be sure your license&#39;s wording will stand up in court.</p>

<h2 id="Unicode"><a class="permalink" href="perlsec.html#Unicode">#</a>Unicode</h2>

<p>Unicode is a new and complex technology and one may easily overlook certain security pitfalls. See <a href="perluniintro.html">perluniintro</a> for an overview and <a href="perlunicode.html">perlunicode</a> for details, and <a href="perlunicode.html#Security-Implications-of-Unicode">&quot;Security Implications of Unicode&quot; in perlunicode</a> for security implications in particular.</p>

<h2 id="Algorithmic-Complexity-Attacks"><a class="permalink" href="perlsec.html#Algorithmic-Complexity-Attacks">#</a><a id="Algorithmic"></a>Algorithmic Complexity Attacks</h2>

<p>Certain internal algorithms used in the implementation of Perl can be attacked by choosing the input carefully to consume large amounts of either time or space or both. This can lead into the so-called <i>Denial of Service</i> (DoS) attacks.</p>

<ul>

<li><p>Hash Algorithm - Hash algorithms like the one used in Perl are well known to be vulnerable to collision attacks on their hash function. Such attacks involve constructing a set of keys which collide into the same bucket producing inefficient behavior. Such attacks often depend on discovering the seed of the hash function used to map the keys to buckets. That seed is then used to brute-force a key set which can be used to mount a denial of service attack. In Perl 5.8.1 changes were introduced to harden Perl to such attacks, and then later in Perl 5.18.0 these features were enhanced and additional protections added.</p>

<p>At the time of this writing, Perl 5.18.0 is considered to be well-hardened against algorithmic complexity attacks on its hash implementation. This is largely owed to the following measures mitigate attacks:</p>

<dl>

<dt id="Hash-Seed-Randomization"><a class="permalink" href="perlsec.html#Hash-Seed-Randomization">#</a><a id="Hash"></a>Hash Seed Randomization</dt>
<dd>

<p>In order to make it impossible to know what seed to generate an attack key set for, this seed is randomly initialized at process start. This may be overridden by using the PERL_HASH_SEED environment variable, see <a href="perlrun.html#PERL_HASH_SEED">&quot;PERL_HASH_SEED&quot; in perlrun</a>. This environment variable controls how items are actually stored, not how they are presented via <code>keys</code>, <code>values</code> and <code>each</code>.</p>

</dd>
<dt id="Hash-Traversal-Randomization"><a class="permalink" href="perlsec.html#Hash-Traversal-Randomization">#</a><a id="Hash1"></a>Hash Traversal Randomization</dt>
<dd>

<p>Independent of which seed is used in the hash function, <code>keys</code>, <code>values</code>, and <code>each</code> return items in a per-hash randomized order. Modifying a hash by insertion will change the iteration order of that hash. This behavior can be overridden by using <code>hash_traversal_mask()</code> from <a href="./Hash::Util.html">Hash::Util</a> or by using the PERL_PERTURB_KEYS environment variable, see <a href="perlrun.html#PERL_PERTURB_KEYS">&quot;PERL_PERTURB_KEYS&quot; in perlrun</a>. Note that this feature controls the &quot;visible&quot; order of the keys, and not the actual order they are stored in.</p>

</dd>
<dt id="Bucket-Order-Perturbance"><a class="permalink" href="perlsec.html#Bucket-Order-Perturbance">#</a><a id="Bucket"></a>Bucket Order Perturbance</dt>
<dd>

<p>When items collide into a given hash bucket the order they are stored in the chain is no longer predictable in Perl 5.18. This has the intention to make it harder to observe a collision. This behavior can be overridden by using the PERL_PERTURB_KEYS environment variable, see <a href="perlrun.html#PERL_PERTURB_KEYS">&quot;PERL_PERTURB_KEYS&quot; in perlrun</a>.</p>

</dd>
<dt id="New-Default-Hash-Function"><a class="permalink" href="perlsec.html#New-Default-Hash-Function">#</a><a id="New"></a>New Default Hash Function</dt>
<dd>

<p>The default hash function has been modified with the intention of making it harder to infer the hash seed.</p>

</dd>
<dt id="Alternative-Hash-Functions"><a class="permalink" href="perlsec.html#Alternative-Hash-Functions">#</a><a id="Alternative"></a>Alternative Hash Functions</dt>
<dd>

<p>The source code includes multiple hash algorithms to choose from. While we believe that the default perl hash is robust to attack, we have included the hash function Siphash as a fall-back option. At the time of release of Perl 5.18.0 Siphash is believed to be of cryptographic strength. This is not the default as it is much slower than the default hash.</p>

</dd>
</dl>

<p>Without compiling a special Perl, there is no way to get the exact same behavior of any versions prior to Perl 5.18.0. The closest one can get is by setting PERL_PERTURB_KEYS to 0 and setting the PERL_HASH_SEED to a known value. We do not advise those settings for production use due to the above security considerations.</p>

<p><b>Perl has never guaranteed any ordering of the hash keys</b>, and the ordering has already changed several times during the lifetime of Perl 5. Also, the ordering of hash keys has always been, and continues to be, affected by the insertion order and the history of changes made to the hash over its lifetime.</p>

<p>Also note that while the order of the hash elements might be randomized, this &quot;pseudo-ordering&quot; should <b>not</b> be used for applications like shuffling a list randomly (use <code>List::Util::shuffle()</code> for that, see <a href="./List::Util.html">List::Util</a>, a standard core module since Perl 5.8.0; or the CPAN module <code>Algorithm::Numerical::Shuffle</code>), or for generating permutations (use e.g. the CPAN modules <code>Algorithm::Permute</code> or <code>Algorithm::FastPermute</code>), or for any cryptographic applications.</p>

<p>Tied hashes may have their own ordering and algorithmic complexity attacks.</p>

</li>
<li><p>Regular expressions - Perl&#39;s regular expression engine is so called NFA (Non-deterministic Finite Automaton), which among other things means that it can rather easily consume large amounts of both time and space if the regular expression may match in several ways. Careful crafting of the regular expressions can help but quite often there really isn&#39;t much one can do (the book &quot;Mastering Regular Expressions&quot; is required reading, see <a href="perlfaq2.html">perlfaq2</a>). Running out of space manifests itself by Perl running out of memory.</p>

</li>
<li><p>Sorting - the quicksort algorithm used in Perls before 5.8.0 to implement the sort() function was very easy to trick into misbehaving so that it consumes a lot of time. Starting from Perl 5.8.0 a different sorting algorithm, mergesort, is used by default. Mergesort cannot misbehave on any input.</p>

</li>
</ul>

<p>See <a href="https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf">https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf</a> for more information, and any computer science textbook on algorithmic complexity.</p>

<h2 id="Using-Sudo"><a class="permalink" href="perlsec.html#Using-Sudo">#</a><a id="Using"></a>Using Sudo</h2>

<p>The popular tool <code>sudo</code> provides a controlled way for users to be able to run programs as other users. It sanitises the execution environment to some extent, and will avoid the <a href="perlsec.html#Shebang-Race-Condition">shebang race condition</a>. If you don&#39;t have the safe version of set-id scripts, then <code>sudo</code> may be a more convenient way of executing a script as another user than writing a C wrapper would be.</p>

<p>However, <code>sudo</code> sets the real user or group ID to that of the target identity, not just the effective ID as set-id bits do. As a result, Perl can&#39;t detect that it is running under <code>sudo</code>, and so won&#39;t automatically take its own security precautions such as turning on taint mode. Where <code>sudo</code> configuration dictates exactly which command can be run, the approved command may include a <code>-T</code> option to perl to enable taint mode.</p>

<p>In general, it is necessary to evaluate the suitability of a script to run under <code>sudo</code> specifically with that kind of execution environment in mind. It is neither necessary nor sufficient for the same script to be suitable to run in a traditional set-id arrangement, though many of the issues overlap.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="perlsec.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="perlrun.html#ENVIRONMENT">&quot;ENVIRONMENT&quot; in perlrun</a> for its description of cleaning up environment variables.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
